package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojo.ApUser;
import com.heima.model.user.vo.UserLoginVO;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.BCrypt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {


    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 登录接口
     * 二合一，用户登录也是有游客登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        System.out.println("进入login");
        //TODO
        //登录接口提交Git仓库
        //1.游客登录
        if (dto.getPhone().equals("") && dto.getPassword().equals("")) {
            Long id = 0L;
            //获取token
            String token = AppJwtUtil.getToken(id);
            //获取data[]对象
            UserLoginVO data = UserLoginVO.builder()
                    .Token(token)
                    .build();
            return ResponseResult.okResult(data);
        }
        //2.用户登录
        if(StringUtils.isNotBlank(dto.getPhone()) && StringUtils.isNotBlank(dto.getPassword())){
            //2.1BCrypt加密
            //2.1.1获取BCrypt获取盐
            String salt = BCrypt.gensalt();
            //2.1.2获取BCrypt加密
            String hashpwd = BCrypt.hashpw(dto.getPassword(), salt);
            String username = dto.getPhone();
            //根据用户和密码查找数据库中存不存在
            LambdaQueryWrapper<ApUser> querywrapper = new LambdaQueryWrapper();
            querywrapper.eq(ApUser::getPhone, username);
            ApUser apUser = apUserMapper.selectOne(querywrapper);
            //AppThreadLocalUtil.setUser(apUser);
            //用户不存在
            if (apUser == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
            }
            //效验密码
            boolean checkpw = BCrypt.checkpw(dto.getPassword(), hashpwd);
            if (!checkpw){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            //判断状态
            if (apUser.getStatus()){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_STATUS_ERROR);
            }
            String token = AppJwtUtil.getToken(apUser.getId().longValue());
            UserLoginVO loginVO = UserLoginVO.builder()
                    .apUser(apUser)
                    .Token(token)
                    .build();
            return ResponseResult.okResult(loginVO);
        }
        return null;
    }

    /**
     * 根据用户id获取用户实体
     *
     * @param userId
     * @return
     */
    @Override
    public ResponseResult<ApUser> findUserById(Integer userId) {
        ApUser apUser = getById(userId);
        return ResponseResult.okResult(apUser);
    }
}
